<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="上级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-tree-select
            ref="treeSelect"
            :disabled="isEdit"
            placeholder="请选择上级分类"
            v-decorator="['pid']"
            dict="biz_goods_category,name,id"
            @change="chooseMenu"
            pidField="pid"
            pidValue="0"
            hasChildField="has_child">
          </j-tree-select>
        </a-form-item>
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>
        <a-form-item label="图标" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="建议上传图片大小为：200*200像素。">
          <j-image-upload :isMultiple="false" v-decorator="['icon']"></j-image-upload>
        </a-form-item>
        <a-form-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['remark']" placeholder="请输入描述"></a-input>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol"  extra="数字越小排序越靠前。">
          <a-input-number v-decorator="['sort',validatorRules.sort]" placeholder="请输入排序" style="width: 100%"/>
        </a-form-item>
        <!--<a-form-item label="级别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['level']" placeholder="请输入级别" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="索引值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['seq']" placeholder="请输入索引值"></a-input>
        </a-form-item>-->
        <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radioButton" v-decorator="['status',validatorRules.status]" :trigger-change="true" dictCode="status" placeholder="请选择是否启用"/>
        </a-form-item>

        <a-form-item label="首页展示" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="isPID">
          <j-dict-select-tag type="radioButton" v-decorator="['isFront',validatorRules.isFront]" :trigger-change="true" dictCode="is_commander_front" placeholder="请选择是否首页展示"/>
        </a-form-item>
        <a-form-item label="团长专属" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="isPID" style="opacity: 0;height: 0">
          <j-dict-select-tag type="radioButton" v-decorator="['isExclusive',validatorRules.isExclusive]" :trigger-change="true" dictCode="is_commander_exclusive" placeholder="请选择是否团长专属"/>
        </a-form-item>
        <!--<a-form-item label="deleted" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['deleted']" :trigger-change="true" dictCode="del_flag" placeholder="请选择deleted"/>
        </a-form-item>-->

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction ,getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JTreeSelect from '@/components/jeecg/JTreeSelect'

  export default {
    name: "GoodsCategoryModal",
    components: {
      JImageUpload,
      JDictSelectTag,
      JTreeSelect
    },
    data () {
      return {
        isPID:false,
        isEdit:false,
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          isFront: {
            initialValue:0,
            rules: [
              { required: false, message: '请选择是否首页展示!'},
            ]
          },
          isExclusive: {
            initialValue:0,
            rules: [
              { required: false, message: '请选择团长专属!'},
            ]
          },
          pid: {
            rules: [
              { required: true, message: '请输入上级分类!'},
            ]
          },
          sort: {
            rules: [
              { required: false, message: '请输入排序!'},
              { pattern: /^[+]{0,1}(\d+)$/, message: '请输入正整数数字!'},
            ]
          },
          name: {
            rules: [
              { required: true, message: '请输入名称!'},
            ]
          },
          status: {
            initialValue:1,
            // rules: [
            //   { required: true, message: '请输入名称!'},
            // ]
          },
        },
        url: {
          add: "/goods/goodsCategory/add",
          edit: "/goods/goodsCategory/edit",
          pid:"goods/goodsCategory/queryByKey/"//查询PID接口
        },
        expandedRowKeys:[],
        pidField:"pid"

      }
    },
    created () {
    },
    methods: {
      chooseMenu(e){
        console.log(e)
        if (e){
          this.isPID = false
        }else {
          this.isPID = true
        }


        /*getAction(this.url.pid+e).then((res)=>{
          console.log(res.result.pid)
          if (res.result.pid == 0){
            this.isPID = true
          }else {
            this.isPID = false
          }
        })*/
      },
      add (obj) {
        this.edit(obj);
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'pid','name','icon','remark','sort','status','isExclusive','isFront'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let old_pid = this.model[this.pidField]
            let formData = Object.assign(this.model, values);
            let new_pid = this.model[this.pidField]
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.isPID = false
        this.close()

      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'pid','name','icon','remark','sort','status','isExclusive','isFront'))
      },
      submitSuccess(formData,flag){
        if(!formData.id){
          let treeData = this.$refs.treeSelect.getCurrTreeData()
          this.expandedRowKeys=[]
          this.getExpandKeysByPid(formData[this.pidField],treeData,treeData)
          this.$emit('ok',formData,this.expandedRowKeys.reverse());
        }else{
          this.$emit('ok',formData,flag);
        }
      },
      getExpandKeysByPid(pid,arr,all){
        if(pid && arr && arr.length>0){
          for(let i=0;i<arr.length;i++){
            if(arr[i].key==pid){
              this.expandedRowKeys.push(arr[i].key)
              this.getExpandKeysByPid(arr[i]['parentId'],all,all)
            }else{
              this.getExpandKeysByPid(pid,arr[i].children,all)
            }
          }
        }
      }


    }
  }
</script>